// pages/enveWithdraw/enveWithdraw.js
var app = getApp();
var timer;
const pushCodeUri = "/EnvePacket/pushCode";
const goWithdrawUri = '/EnvePacket/goWithdrawV2';
const checkWithdrawUri = '/EnvePacket/checkUserWithdraw';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: 1,//是否显示加载中图标
    withdraw_success_title:'',//领取成功弹层的文案
    phoneEndNumber:'****',//手机尾号
    recommendList: [],
    isWithdrawalsSuccessShow: 0,//控制提现成功弹层是否显示
    isBalancePaymentShow: 0,//控制支付弹层是否显示
    winWidth: 320,//获取手机屏幕的宽度
    winHeight: 510,//获取手机屏幕的高度
    hosList: [],//存储列表数据
    pageFile: 1,//初始化页码
    balance: '0.00',//用户已领导的总余额
    btnText: "获取验证码",//验证码发送
    btnDisplay: false,//发送验证码按钮是否禁用
    time: '60',
    phone: "",//手机号
    is_bind_phone: 0,//是否绑定过了手机 1 表是已绑定 0 表示未绑定
    phone_input_display: false, //是否禁止用户输入手机号
    codeBoxShow: 0,//验证码模块是否显示 0 不显示 1 显示
    withdrawIsOk: 0,//提现按钮是否激活 0 表示未激活 1 表示已激活
    code: ""//验证码
  },
  withdrawalsCloseBtn: function () {
    var _this = this;
    _this.getUserAmount();
    _this.setData({
      isWithdrawalsSuccessShow: 0
    })
  },
  // 关闭提现弹层
  cancle:function(){
    this.setData({
      codeBoxShow:0
    })
  },
  //跳会员页
  goOpenVip: function (e) {
    var _this = this;
    wx.navigateTo({
      url: '../openVip/openVip',
    });
  },
  //跳首页
  goHome: function (e) {
    var _this = this;
    wx.reLaunch({
      url: '../home/home',
    });
  },
  //用户余额
  getUserAmount: function () {
    var _this = this;
    var requestData = {
      version: app.setConfig.version,
      type: app.setConfig.type,
      token: _this.data.token
    }
    app.post('/User/muWalletV2', requestData, function (res) {
      if (res.code == 2000) {
        var phone = _this.data.phone;
        if(phone==''){
          phone=res.data.phone;
        }else{
          phone = _this.data.phone;
        }
        _this.setData({
          isLoading:0,
          withdraw_success_title: res.data.withdraw_success_title,
          head_img: res.data.head_img,
          nick_name: res.data.nick_name,
          amount: res.data.amount,
          is_bind_phone: res.data.is_bind_phone,
          phone: phone,
          phoneEndNumber: res.data.phone.substring(res.data.phone.length - 4)
        });
        if (res.data.is_bind_phone == 1) {
          _this.setData({
            phone_input_display: true
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    })
  },
  // 获取热词文档列表
  getHotRecommend: function () {
    var _this = this;
    var configs = wx.getStorageSync('configs');
    console.log(configs);
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      keywords: configs.key_words,
      search_type: 0,
      page: _this.data.page
    }
    app.post('/Goods/recommend', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var recommendList = _this.data.recommendList;
        for (var i = 0; i < res.data.list.length;i++){
          res.data.list[i].img = configs.static_url_http + '/' + res.data.list[i].img;
        }
        recommendList = recommendList.concat(res.data.list);
        _this.setData({
          page: res.data.page,
          recommendList: recommendList
        })

      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var configs = wx.getStorageSync('configs');
    _this.setData({
      filesSourceUri: configs.static_url_http,
    })
    if (options.gainSuccess == 1 && options.gainSuccess != undefined) {
      _this.setData({
        isWithdrawalsSuccessShow: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;

    wx.getSystemInfo({
      success: function (res) {
        var theWinHeight = res.windowHeight;
        var theWinWidth = res.windowWidth;
        _this.setData({
          winHeight: theWinHeight,
          winWidth: theWinWidth
        });
      }
    });
    _this.loop();
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    var theSysStatus = wx.getStorageSync('sysStatus');
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken,
        sysStatus: theSysStatus,
        pageFile: 1
      });
      _this.getUserAmount();
      _this.getHotRecommend();
    }
  },
  /*发送验证码*/
  pushCodeEvent: function (e) {
    var that = this;
    var formId = e.detail.formId;
    app.postFormId(that.data.token, formId);
    if(that.data.phone==''){
      wx.showToast({
        title: '请输入手机号',
        icon: "none",
        duration: 2000
      });
    }else{
      //禁用按钮
      that.setData({
        btnDisplay: true
      });
      //启动定时器
      if (timer) {
        clearInterval(timer);
      }

      var requestData = {
        token: that.data.token,
        version: app.setConfig.version,
        type: app.setConfig.type,
        phone: that.data.phone
      }
      app.post(pushCodeUri, requestData, (res) => {
        if (res.code == 2000) {
          wx.showToast({
            title: '验证码发送成功',
            icon: "none",
            duration: 2000
          });
          timer = setInterval(() => {
            var timeNow = that.data.time
            if (timeNow > 0) {
              that.setData({
                time: timeNow - 1,
                btnText: '(' + (timeNow - 1) + "s) 重新获取"
              })
            } else {
              clearInterval(timer);
              that.setData({
                time: 60,
                btnText: "重新获取",
                btnDisplay: false
              })
            }
          }, 1000)
        } else {
          clearInterval(timer);
          that.setData({
            time: 60,
            btnText: "获取验证码",
            btnDisplay: false
          })
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  //输入手机号
  phoneInputEvent: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //输入验证码
  codeInputEvent: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 点击页面中提现按钮
  goWithdrawPage:function(e){
    var that = this;
    var is_bind_phone = that.data.is_bind_phone;
    var formId = e.detail.formId;
    app.postFormId(that.data.token, formId);
    var requestData = {
      token: that.data.token,
      version: app.setConfig.version,
      type: "site_type"
    }
    app.post(checkWithdrawUri, requestData, (res) => {
      if (res.code == 2000) {
          that.setData({ codeBoxShow: 1});
        // }
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        })
      }
    });
  },
  // 点击弹层中的提现按钮
  goWithdrawMask: function (e) {
    var that = this;
    var is_bind_phone = that.data.is_bind_phone;
    var formId = e.detail.formId;
    app.postFormId(that.data.token, formId);
    if(that.data.phone==''){
      wx.showToast({
        title: '请输入手机号',
        icon: "none",
        duration: 2000
      });
    } else if (that.data.code == ''){
      wx.showToast({
        title: '请输入验证码',
        icon: "none",
        duration: 2000
      });
    }else{
      if (that.data.code) {
        var requestData = {
          token: that.data.token,
          version: app.setConfig.version,
          code: that.data.code,
          phone: that.data.phone
        };
        app.post(goWithdrawUri, requestData, (res) => {
          if (res.code == 2000) {
            that.setData({
              codeBoxShow: 0,
              isWithdrawalsSuccessShow: 1
            })
            setTimeout(function () { that.loop() }, 2000);
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000
            })
          }
        });
      } else {
        wx.showToast({
          title: '请填写验证码',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getHotRecommend();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})